package com.koudai.reader;

import java.util.ArrayList;
import java.util.List;

import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.koudai.reader.android.R;
import com.koudai.reader.conponment.ExistDialog;
import com.koudai.reader.conponment.SelectTypeDialog;
import com.koudai.reader.util.Constant;
import com.koudai.reader.vo.EndUser;

public class BookCityActivity extends TabActivity implements OnCheckedChangeListener {

    RadioGroup radios;
    TabHost tabs;
    private ImageView re_select_type;

    private List<Integer> selectedTypeList = new ArrayList<Integer>();

    TextView score;
    ImageView isVip;

    private Handler handler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 1:
                selectedTypeList.clear();
                SharedPreferences config = getSharedPreferences("config", 0);
                String[] selectSortType = config.getString("select_sort_type", "0").split(",");
                for (String type : selectSortType) {
                    selectedTypeList.add(Integer.valueOf(type));
                }
                initView();
                break;

            }
        };
    };

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        score.setText(EndUser.getInstance().getGlodStr());
        
        isVip = (ImageView) findViewById(R.id.vip);
        if (EndUser.getInstance().isVip()) {
            isVip.setVisibility(View.VISIBLE);    
        } else {
            isVip.setVisibility(View.INVISIBLE);    
        }
        
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // 取得用户设置的三个兴趣爱好
        SharedPreferences config = getSharedPreferences("config", 0);
        String[] selectSortType = config.getString("select_sort_type", "0").split(",");

        setContentView(R.layout.books_in_sort);
        radios = (RadioGroup) findViewById(R.id.books_sort_tab);
        re_select_type = (ImageView) findViewById(R.id.re_select_sort);
        re_select_type.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new SelectTypeDialog(BookCityActivity.this, handler).show();
            }
        });

        score = (TextView) findViewById(R.id.score);
        
        
        if (selectSortType.length < 3) {

            new SelectTypeDialog(this, handler).show();

        } else {

            for (String type : selectSortType) {
                selectedTypeList.add(Integer.valueOf(type));
            }

            initView();
        }

    }

    private TabSpec tab1;
    private TabSpec tab2;
    private TabSpec tab3;
    private TabSpec tab4;

    private void initView() {
        tabs = getTabHost();

        Intent intent1 = new Intent(BookCityActivity.this, TopBookListActivity.class);
        intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent1.putExtra("sort_type", 0);
        
        if (tab1 == null) {
            tab1 = tabs.newTabSpec("1").setIndicator("", null).setContent(intent1);
            tabs.addTab(tab1);
        } else {
            tab1.setContent(intent1);
        }

        Intent intent2 = new Intent(BookCityActivity.this, TopBookListActivity.class);
        intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent2.putExtra("sort_type", selectedTypeList.get(0));
        
        if (tab2 == null) {
            tab2 = tabs.newTabSpec("2").setIndicator("", null).setContent(intent2);
            tabs.addTab(tab2);
        } else {
            tab2.setContent(intent2);
        }
        Intent intent3 = new Intent(BookCityActivity.this, TopBookListActivity.class);
        intent3.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent3.putExtra("sort_type", selectedTypeList.get(1));
        
        if (tab3 == null) {
            tab3 = tabs.newTabSpec("3").setIndicator("", null).setContent(intent3);
            tabs.addTab(tab3);
        } else {
            tab3.setContent(intent3);
        }
        Intent intent4 = new Intent(BookCityActivity.this, TopBookListActivity.class);
        intent4.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent4.putExtra("sort_type", selectedTypeList.get(2));
        
        if (tab4 == null) {
            tab4 = tabs.newTabSpec("4").setIndicator("", null).setContent(intent4);
            tabs.addTab(tab4);
        } else {
            tab4.setContent(intent4);
        }
        radios = (RadioGroup) findViewById(R.id.books_sort_tab);
        ((RadioButton) radios.findViewById(R.id.rb_sort_sub_1)).setText(Constant.mainCategoryMapping.get(selectedTypeList.get(0)));
        ((RadioButton) radios.findViewById(R.id.rb_sort_sub_2)).setText(Constant.mainCategoryMapping.get(selectedTypeList.get(1)));
        ((RadioButton) radios.findViewById(R.id.rb_sort_sub_3)).setText(Constant.mainCategoryMapping.get(selectedTypeList.get(2)));

        radios.setOnCheckedChangeListener(this);

        ImageView searchBtn = (ImageView) findViewById(R.id.search_btn);
        searchBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View paramView) {

                Intent intent = new Intent();
                intent.setClass(BookCityActivity.this, SearchBookActivity.class);
                BookCityActivity.this.startActivity(intent);
            }
        });
        
        radios.check(R.id.rb_sort_alle);
        tabs.setCurrentTabByTag("1");
    }

    @Override
    public void onCheckedChanged(RadioGroup arg0, int arg1) {
        switch (arg1) {
        case R.id.rb_sort_alle:
            tabs.setCurrentTabByTag("1");
            break;
        case R.id.rb_sort_sub_1:
            tabs.setCurrentTabByTag("2");
            break;
        case R.id.rb_sort_sub_2:
            tabs.setCurrentTabByTag("3");
            break;
        case R.id.rb_sort_sub_3:
            tabs.setCurrentTabByTag("4");
            break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                ExistDialog.showDialog(this.getParent() == null ? this : this.getParent());
            }
        }

        return super.onKeyDown(keyCode, event);
    }

}
